---------------------------------------------------------------
-- @Description：         二号位逻辑
---------------------------------------------------------------
local skynet = require "skynet"
local log = require "sky_log"
local gg = require "game_utils"
local uu = require "sky_utils"

local M = {}

--有当前轮花色逻辑
local function do_round_color(handcards, prev_card, enemy_no_color_t, outcard_history, main_color, outcard_history_more, m_seatid)
    local card
    local color = uu.calc_card_color(prev_card)
    
    if not gg.check_handcards_bigger_card_pure(handcards, prev_card, color) then 
        card = uu.get_color_min_card(handcards, color)
        if card then
            uu.debug_logic(1)
            return card
        else
            log.error("[second] get_color_min_card error!")
        end
    end

    --对手没有该花色
    if gg.check_enemy_has_no_color(enemy_no_color_t, color, m_seatid, outcard_history_more, true, handcards, outcard_history) then 
        card = uu.check_handcards_bigger_card_min(handcards, prev_card)
        if card then
            uu.debug_logic(2)
            return card
        else
            --log.error("[second] check_handcards_bigger_card_min error!")
            card = uu.get_color_min_card(handcards, round_color)
        end
    end

    --对手有该花色时检测自己是否有第二大的牌
    local second_card = uu.history_check_second_card(handcards, color, outcard_history)
    if not second_card then 
        card = uu.check_handcards_bigger_card_max(handcards, prev_card, main_color)
        if not card then 
            log.error("[second] check_handcards_bigger_card_max error!")
        end
        uu.debug_logic(3)
    else
        card = uu.check_handcards_small_card_max(handcards, second_card)
        if not card then
            card = second_card
        end
        uu.debug_logic(4)
    end

    return card
end

--无当前轮花色逻辑
local function do_no_round_color(handcards, main_color, round_color, enemy_no_color_t, outcard_history, outcard_history_more, m_seatid)
    local card
    
    local main_color_count = gg.get_main_card_count(handcards, main_color)
    --有主牌逻辑
    if main_color_count>0 then
        local t = uu.get_cards_except_t(handcards, {13, 14}, main_color)
        if next(t) then
            if gg.check_enemy_has_no_color(enemy_no_color_t, round_color, m_seatid, outcard_history_more, true, handcards, outcard_history)==true then 
                if gg.check_enemy_has_no_color(enemy_no_color_t, main_color, m_seatid, outcard_history_more, true, handcards, outcard_history)~=true then
                    card = t[1]
                    if card then
                        uu.debug_logic(5)
                        return card
                    else
                        log.error("[second] do_no_round_color error!")
                    end
                end
            end

            card = uu.get_color_min_card(handcards, main_color)
            if not card then
                log.error("[second] get_color_min_card error!")
            else
                return card
            end
        end 
    end

    --无主牌或者除了主牌A,K外无其他主牌逻辑
    local the_color = gg.check_enemy_has_no_color_s_and_main_card(handcards, {1,2,3,4}, enemy_no_color_t, main_color)
    if the_color then 
        local max_card = uu.history_check_max_card(handcards, the_color, outcard_history)
        local count = uu.get_color_cards_count(handcards, the_color)
        if max_card and count >= 2 then
            card = uu.get_color_min_card(handcards, the_color)
            if card then
                uu.debug_logic(6)
                return card
            else
                log.error("[second] get_color_min_card error!")
            end
        end
    end

    card = gg.history_check_more_color_or_min_card_expect_maincards(handcards, outcard_history, main_color)
    if not card then
        card = uu.get_color_min_card(handcards, main_color)
    end

    uu.debug_logic(7)
    return card
end

function M.do_card(handcards, main_color, round_color, prev_card, outcard_history, enemy_no_color_t, outcard_history_more, m_seatid)
    if __LOG_LEVEL___==1 then
        local tmp = {}
        for _, v in ipairs(handcards) do
            table.insert(tmp, string.format("%04X", v))
        end
        uu.debug_logic(tmp)
    end

    local card

    if gg.check_has_round_color(handcards, round_color) then 
        card = do_round_color(handcards, prev_card, enemy_no_color_t, outcard_history, main_color, outcard_history_more, m_seatid)
    else
        card = do_no_round_color(handcards, main_color, round_color, enemy_no_color_t, outcard_history, outcard_history_more, m_seatid)
    end

    --最后的容错
    local the_flag, the_card = gg.check_error_card(handcards, card, round_color, 2)
    if the_flag==true then
        uu.debug_logic(8)
        card = the_card
    end

    return card
end


return M